package com.eff.android;

import java.util.List;

import com.eff.android.fragment.main.EMainFragment;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class EMainActivity extends EBaseActivity{

	 	public static String TAG = EMainActivity.class.getSimpleName();

	    private FragmentManager mFragmentManager;
	    private FragmentTransaction mFragmentTransaction;
	    private String mCurrentFragmentTag;
	    
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.e_activity_main);
	
			mFragmentManager = getSupportFragmentManager();
	
			configActionBar();
			Bundle bundle = getIntent().getExtras();
			initFragment(true, bundle);
	    }

	    private void configActionBar() {
			ActionBar ab = getActionBar();
			ab.setDisplayShowHomeEnabled(false);
			ab.setHomeButtonEnabled(false);
			ab.setDisplayHomeAsUpEnabled(false);
			ab.setDisplayUseLogoEnabled(false);
			ab.setDisplayShowTitleEnabled(false);
			ab.setDisplayShowCustomEnabled(true);
			ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
	    }

	    private void initFragment(boolean isFirstRunning, Bundle bundle) {
			if(isFirstRunning)
				setupContentViewWithNoBackStack(
					EMainFragment.newInstance(this, null), EMainFragment.TAG);
			else
			    setupContentViewWithNoBackStack(
					EMainFragment.newInstance(this, bundle), EMainFragment.TAG);
		    
	    }

	    // ---------------------------------------------------------------------------------

	    public void setupContentViewWithNoBackStack(Fragment fragment,
		    String fragmentTag) {
			setupContentView(fragment, fragmentTag, false);
			printFragments();
	    }

	    public void setupContentView(Fragment fragment, String fragmentTag) {
	    	setupContentView(fragment, fragmentTag, true);
	    	printFragments();
	    }

	    private void setupContentView(Fragment fragment, String fragmentTag,
		    boolean isAddToBackStack) {
			if (mCurrentFragmentTag != null
				&& !mCurrentFragmentTag.equalsIgnoreCase(fragmentTag))
			    detachFragment(getFragmentByTag(mCurrentFragmentTag), false);
	
			mCurrentFragmentTag = fragmentTag;
			attachFragment(R.id.content_frame_options, fragment, mCurrentFragmentTag,
				isAddToBackStack, false);
			commitTransactions();
	    }

	    // ---------------------------------------------------------------------------------
	    // CONTENT FRAGMENT REGION
	    public void setupContentFragment(Fragment fragment, String tagName,
		    boolean isAddToBackStack) {
//			attachFragment(mLeftMenu.getContentContainer().getId(), fragment,
//			tagName, isAddToBackStack);
			commitTransactions();
	
			printFragments();
	    }

	    private void attachFragment(int layout, Fragment f, String tag,
		    boolean isAddToBackStack, boolean shouldAnimate) {
		mFragmentManager.executePendingTransactions();
		if (f != null) {
		    if (f.isDetached()) {
			ensureTransaction(shouldAnimate);
			mFragmentTransaction.attach(f);
			if (isAddToBackStack)
			    mFragmentTransaction.addToBackStack(tag);
		    } else {
			ensureTransaction(shouldAnimate);
			mFragmentTransaction.replace(layout, f, tag);
			if (isAddToBackStack)
			    mFragmentTransaction.addToBackStack(tag);
		    }
		}
	    }

	    private FragmentTransaction ensureTransaction(boolean shouldAnimate) {
		if (mFragmentTransaction == null) {
		    mFragmentTransaction = mFragmentManager.beginTransaction();

		    if (shouldAnimate) {
			// TODO: animation revise
			// mFragmentTransaction
			// .setCustomAnimations(R.anim.bd_slide_in_right, 0,
			// R.anim.bd_slide_in_left, 0);
		    } else {
			mFragmentTransaction.setCustomAnimations(0, 0, 0, 0);
		    }
		}
		return mFragmentTransaction;
	    }

	    public Fragment getFragmentByTag(String tag) {
		Fragment f = mFragmentManager.findFragmentByTag(tag);
		return f;
	    }

	    public void clearAllFragmentsInStack() {
		mFragmentManager.popBackStack(null,
			FragmentManager.POP_BACK_STACK_INCLUSIVE);
	    }

	    private void detachFragment(Fragment f) {
		if (f != null && !f.isDetached()) {
		    ensureTransaction(false);
		    mFragmentTransaction.detach(f);
		}
	    }

	    private void detachFragment(Fragment f, boolean shouldAnimate) {
		if (f != null && !f.isDetached()) {
		    ensureTransaction(shouldAnimate);
		    mFragmentTransaction.detach(f);
		}
	    }

	    private void commitTransactions() {
		if (mFragmentTransaction != null && !mFragmentTransaction.isEmpty()) {
		    mFragmentTransaction.commitAllowingStateLoss();
		    mFragmentTransaction = null;
		}
	    }

	    private void printFragments() {
			List<Fragment> fs = mFragmentManager.getFragments();
			StringBuilder sb = new StringBuilder();
			int index = 0;
			if (fs != null) {
			    for (Fragment f : fs) {
					if (f != null) {
					    sb.append(String.format("%d - %s; ", index++, f.getTag()));
					}
			    }
			}
	    }
}
